<?php

namespace App\Http\Controllers\Home;

use Mail;
use App\SMS\M3Result;
use App\Model\HomeUser;
use App\SMS\SendTemplateSMS;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
    //
    public function phoneReg()
    {
    	return view('home.phoneRegister');
    }

    public function sendCode(Request $request)
    {
    	// 
    	$phone = $request->phone;

    	$code = rand(1000,9999);
    	$arr = [$code,5];

    	$templateSMS = new SendTemplateSMS();
    	$M3Result = new M3Result();

    	$M3Result = $templateSMS->SendTemplateSMS($phone,$arr,1);

    	session()->put('phone',$code);

    	return $M3Result->toJson();
    }

    public function register()
    {
    	return view('home.emailRegister');
    }

    public function doEmailRegister(Request $request)
    {
    	$info = $request->execpt('_token');

    	$info['password'] = Hash::check($info['password']);
    	$info['email'] = $info['user_name'];
    	$info['token'] = md5($info['email'].$info['password'].'123');
    	$info['expire'] = time()+3600*24;

    	$user = HomeUser::insert($info);

    	if ($user) {
    		Mail::send('email.active',['user'=>$user],function($m) use ($user){
    			$m->to($user->email,$user->name)->subject('激活邮箱');
    		});

    		return redirect('login')->with('active','请先登录邮箱激活账号');
    	}else{
    		return redirect('emailRegister');
    	}
    }

    public function active(Request $request)
    {
        $user = HomeUser::findOrFail($request->userid);

        if ($request->token != $user->token) {
            return '当前链接非有效链接，请确保您是通过邮箱的激活链接进行激活的！';
        }

        if (time() > $user->expire) {
            return '激活链接已超时，请重新注册';
        }

        $res = $user->update(['active'=>1]);

        if ($res) {
            return redirect('login')->with('msg','账号激活成功');
        }else{
            return '邮箱激活失败，请检查激活链接，或者重新注册账号';
        }
    }

    public function forget()
    {
        return view('home.forget');
    }

    public function doForget(Request $request)
    {
        $email = $request->email;
        $user = HomeUser::where('user_name',$email)->first();

        if ($user) {
            Mail::send('email.forget',['user'=>$user],function($m) use ($user){
                $m->to($user->email,$user->name)->subject('找回密码');
            });
            return '请登录您的邮箱查看重置密码邮件，重新设置密码';
        }else{
            return '用户不存在，请重新输入要找回密码的账号';
        }
    }

    public function reset(Request $request)
    {
        $info = $request->all();

        $user = HomeUser::find($info['uid']);

        return view('home.reset',compact('user'));
    }

    public function doReset(Request $request)
    {
        $info = $request->all();

        $password = Hash::check($info['password']);

        // $res = HomeUser::where('user_name',$info['user_name'])->update('password'=>$password);

        if ($res) {
            return redirect('login');
        }else{
            return redirect('reset');
        }
    }
}
